﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BI.CF.BLL;
using BI.CF.Model;
using System.Web.Helpers;
using System.Net;
using System.Data.SqlClient;
using BI.CF.UI.Models;
using System.Net.Sockets;
using BI.CF.PageList;
using System.Data;
using BI.CF.Common;
using NPOI;
using Excle.Common;
using System.Text;

namespace BI.CF.Company.Controllers
{
    public class MyAccountController : Controller
    {

        //
        // GET: /MyAccount/
        UserBLL Ubll = new UserBLL();
        PropertyBLL bll = new PropertyBLL();
        LoginRecord_BLL Lbll = new LoginRecord_BLL();
        UsersBLL UsersBll = new UsersBLL();
        MyaccountBLL Mbll = new MyaccountBLL();
        public ActionResult MyAccountIndex()
        {
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            Session["Umodel"] = Ubll.User_Login(Umodel.UserName, Umodel.UserPwd);
            MyAccountIndex(Umodel.UserId);
            return View();

        }
        [HttpPost]
        public void MyAccountIndex(int id)
        {

            MemPropertyModel Property = bll.MyaccountIndex(id);
            ViewBag.AccountTotal = Property.AccountTotal;
            ViewBag.AvailableBalance = Property.AvailableBalance;
            ViewBag.FrozenTotal = Property.FrozenTotal;
            ViewBag.IncomeSum = Property.IncomeSum;

        }
        /// <summary>
        /// 登陆页面
        /// </summary>
        /// <returns></returns>
        public ActionResult View_UserLogin()
        {
            Session.Clear();
            return View();
        }

        public ActionResult VIEW_AccountRecharge()
        {
            return View();
        }
        /// <summary>
        /// 登陆方法+获取IP添加到IP信息
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="model">IP信息表</param>
        /// <returns></returns>
        public int View_UserLoginDo(string name, string pwd)
        {
            LoginRecordModel model = new LoginRecordModel();
            UserrsModel Umodel = Ubll.User_Login(name, pwd);
            if (!string.IsNullOrEmpty(Umodel.UserName))
            {
                Session["Umodel"] = Umodel;
                TempData["Uname"] = Umodel.UserName;
                Session["UserID"] = Umodel.UserId;
            }
            string svraddress = "";
            System.Net.IPHostEntry ipentry = System.Net.Dns.GetHostEntry(Dns.GetHostName());
            for (int icount = 0; icount < ipentry.AddressList.Length; icount++)
            {
                if (ipentry.AddressList[icount].AddressFamily == AddressFamily.InterNetwork)
                {
                    svraddress = ipentry.AddressList[icount].ToString();
                    break;
                }
            }
            model.LIP = svraddress;
            model.LTime = DateTime.Now;
            model.OperationRsult = "正常登录";
            model.UserId = Convert.ToInt32(Session["UserID"]);
            if (Session["UserID"] != null)
            {
                Session["Uname"] = name;
                if (Lbll.AddLoginRecord(model) == true)
                {
                    return 1;
                }
                else
                {
                    model.LIP = svraddress;
                    model.LTime = DateTime.Now;
                    model.OperationRsult = "登陆密码错误";
                    model.UserId = Convert.ToInt32(Session["UserID"]);
                    return 0;
                }
            }
            else
            {
                return 0;
            }


        }
        /// <summary>
        /// ip的查询分页
        /// </summary>
        /// <returns></returns>
        public ActionResult View_UserIPShow()
        {
            return View();
        }
        /// <summary>
        /// 显示IP
        /// </summary>
        public string View_UserIPShow3()
        {
            string TimeMax = Request["datemax"].ToString();
            string TimeMin = Request["datemin"].ToString();
            string State = Request["state"].ToString();
            int Uid = Convert.ToInt32(Session["UserID"]);
            int pageindex = int.Parse(Request["pageindex"]);
            int pagesize = int.Parse(Request["pagesize"]);
            Pager<LoginRecordModel> list = Lbll.ShowLoginRecord(TimeMin, TimeMax, "", Uid, pageindex, pagesize);
            string str = JsonHelper.JsonSerializer<Pager<LoginRecordModel>>(list);
            return str;
        }

        public ActionResult View_GetLoginIP(string IP)
        {
            return View();
        }
        /// <summary>
        /// 注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult View_Register()
        {
            return View();
        }
        /// <summary>
        /// 注册方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int View_RegisterDo(string name, string pwd)
        {
            if (Ubll.User_Register(name, pwd) == true)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 注册用户是否重复
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns></returns>
        public int User_RegisterRepeat()
        {
            string name = Request["name"].ToString();
            if (Ubll.User_RegisterRepeat(name) != 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// <summary>
        /// 对比用户支付信息
        /// </summary>
        /// <param name="BankCard"></param>
        /// <returns></returns>
        public int TestBankCard()
        {
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            string BankCard = Request["BankCard"].ToString();
            if (BankCard != Umodel.UserrBcardMesId)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
        /// <summary>
        /// 对比用户支付密码
        /// </summary>
        /// <returns></returns>
        public int TestPayPwd()
        {
            int Paypwd = int.Parse(Request["Paypwd"].ToString());
            decimal Money = Convert.ToDecimal(Request["Money"]);
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            if (Umodel.PaymentPwd == Paypwd && Convert.ToInt32(Session["PhoneCodeString"]) == Convert.ToInt32(Request["PhonePwd"]))
            {
                if (bll.MemPropertyUpd(Money, "线上", DateTime.Now, Umodel.UserId) != 0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPhonePwd()
        {
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            YunPianSmsClient YunPian = new YunPianSmsClient();
            if (Umodel.UserPhone != null)
            {
                YunPian.SendSms(Umodel.UserPhone);

                return Json(Convert.ToInt32(Session["PhoneCodeString"]));
            }
            else
            {
                ///跳转到完善信息页面
                return Json("0");
            }
        }
        /// <summary>
        /// 对比验证码
        /// </summary>
        /// <returns></returns>
        public JsonResult TestPhonePwd()
        {
            string PhonePwd = Request["PhonePwd"].ToString();
            if (Session["PhoneCodeString"].ToString() == PhonePwd)
            {
                return Json("1");
            }
            else
            {
                return Json("0");
            }
        }
        /// <summary>
        /// 显示用户充值记录
        /// </summary>
        /// <returns></returns>
        public ActionResult View_RechargeRecordShow()
        {
            return View();
        }

        /// <summary>
        /// 分页显示充值记录
        /// </summary>
        /// <returns></returns>
        public JsonResult RechargeRecordPage()
        {

            decimal MinMoney = 0;
            decimal MaxMoney = 0;
            int index = Convert.ToInt32(Request["Index"]);
            int size = Convert.ToInt32(Request["Size"]);
            DateTime MinTime = Convert.ToDateTime("1997-01-01");
            DateTime MaxTime = Convert.ToDateTime("2017-02-28");
            if (Request["MinTime"] != null && Request["MinTime"].ToString() != "")
            {
                MinTime = Convert.ToDateTime(Request["MinTime"]);
            }
            if (Request["MaxTime"] != null && Request["MaxTime"].ToString() != "")
            {
                MaxTime = Convert.ToDateTime(Request["MaxTime"]);
            }
            if (Request["MinMoney"] != null)
            {
                MinMoney = Convert.ToDecimal(Request["MinMoney"]);
            }
            if (Request["MaxMoney"] != null)
            {
                MaxMoney = Convert.ToDecimal(Request["MaxMoney"]);
            }
            Pager<RechargeRecordModel> list = Mbll.ShowRechargeRecord(MinMoney, MaxMoney, MinTime, MaxTime, "线上", 0, index, size);
            return Json(list);
        }
        #region Excle导出
        /// <summary>
        /// 导出到Excle
        /// </summary>
        /// <returns></returns>
        public ActionResult OutExcleAll()
        {
            //此处写SQL
            StringBuilder sql=new StringBuilder();
            sql.Append("SELECT RechargeRecord.*,Users.UserName FROM RechargeRecord INNER JOIN  Users ON RechargeRecord.UserrId=Users.UserId WHERE  1=1 ");
            if (Request["MinTime"] != null && Request["MinTime"].ToString() != "")
            {
                sql.Append(" AND RechargeTime>='");
                sql.Append(Request["MinTime"].ToString());
                sql.Append("'");
            }
            if (Request["MaxTime"] != null && Request["MaxTime"].ToString() != "")
            {
                 sql.Append(" AND RechargeTime<='");
                sql.Append(Request["MaxTime"].ToString());
                sql.Append("'");
            }
            if (Request["MinMoney"] != null)
            {
                sql.Append(" AND RechargeMoney>=");
                sql.Append(Request["MinMoney"]);
            }
            if (Request["MaxMoney"] != null)
            {
                sql.Append(" AND RechargeMoney<=");
                sql.Append(Request["MaxMoney"]);
            }
            DataTable _daochuDTcon = Excle.DBHelper.GetDataTable(sql.ToString());
            if (_daochuDTcon != null)
            {
                try
                {
                    if (_daochuDTcon.Rows.Count > 0)
                    {
                        string _excelName = "导出到Execle";//Excel表头名称
                        string fileName = _excelName + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"; //Excel文件名称
                        //调用导出方法
                        //_daochuDTcon Datatable   
                        //_excelName表格头部标题
                        //文件名称
                        NPIOExcleHelper.ExportByWeb(_daochuDTcon, _excelName, fileName);
                        
                    }
                }
                catch (Exception ee)
                {
                    string _err = ee.Message;
                }
            }
            return View("View_RechargeRecordShow");
        }

        #endregion
        /// <summary>
        /// 用户安全中心
        /// </summary>
        /// <returns></returns>
        public ActionResult MyaccountSecurityCenter()
        {
            if (Session["Umodel"] as UserrsModel == null)
            {
                return View("View_UserLogin");
            }
            return View();
        }
        /// <summary>
        /// 用户的安全等级
        /// </summary>
        /// <returns></returns>
        public JsonResult SecurityCenterTesting()
        {
            UserrsModel Umodel = Session["Umodel"] as UserrsModel;
            return Json(Umodel);
        }

        /// <summary>
        /// 用户支付密码修改页
        /// </summary>
        /// <returns></returns>
        public ActionResult PayPwd()
        {
            UserrsModel Umodel = Session["UModel"] as UserrsModel;//获取当前用户
            return View(Umodel);
        }


        /// <summary>
        /// 支付密码修改(无密码，首次设定)
        /// </summary>
        /// <returns></returns>
        public ActionResult PayPwdEdit(UserrsModel m)
        {
            UserrsModel Umodel = Session["UModel"] as UserrsModel;//获取当前用户
            m.UserId = Umodel.UserId;

            if (Ubll.UserPayPwd_Upd(m) > 0)
            {
                return Json(1);
            }
            else
            {
                return Json(0);
            }
        }


        /// <summary>
        /// 支付密码修改（有密码，修改）
        /// </summary>
        /// <returns></returns>
        public ActionResult PayPwdEdit2(UserrsModel m)
        {
            UserrsModel Umodel = Session["UModel"] as UserrsModel;//获取当前用户
            m.UserId = Umodel.UserId;

            if (Ubll.UserPayPwd_Upd(m) > 0)
            {
                return Json(1);
            }
            else
            {
                return Json(0);
            }
        }


        /// <summary>
        /// 支付密码修改前验证
        /// </summary>
        /// <returns></returns>
        public ActionResult PwdQuery()
        {
            int pwd = Convert.ToInt32(Request["PaymentPwd"]);
            int id = Convert.ToInt32(Request["Userid"]);

            if (Ubll.PwdQuery(pwd, id) > 0)
            {
                return Json(1);
            }
            else
            {
                return Json(0);
            }
        }

    }
}
